/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.shop.api.entity.GoodsMaterial;
import com.pig4cloud.pigx.shop.mapper.GoodsMaterialMapper;
import com.pig4cloud.pigx.shop.service.GoodsMaterialService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 素材库
 *
 * @author pig code generator
 * @date 2023-10-08 11:03:27
 */
@Slf4j
@Service
@AllArgsConstructor
public class GoodsMaterialServiceImpl extends ServiceImpl<GoodsMaterialMapper, GoodsMaterial>
		implements GoodsMaterialService {

	private final RedisTemplate<String, String> redisTemplate;

	/**
	 * 添加图片
	 * @param goodsMaterial
	 * @return
	 */
	@Override
	public R add(GoodsMaterial goodsMaterial) {
		this.save(goodsMaterial);
		Long id = goodsMaterial.getId();
		// 缓存图片路径
		redisTemplate.opsForValue().set("image_" + id, goodsMaterial.getMaterialUrl());
		return R.ok();
	}

	/**
	 * 修改图片
	 * @param goodsMaterial
	 * @return
	 */
	@Override
	public R update(GoodsMaterial goodsMaterial) {
		this.updateById(goodsMaterial);
		Long id = goodsMaterial.getId();
		// 缓存图片路径
		redisTemplate.opsForValue().set("image_" + id, goodsMaterial.getMaterialUrl());
		return R.ok();
	}

	/**
	 * 将数据库图片ID转成图片地址
	 * @param ids
	 * @return
	 */
	@Override
	public String convertImageIdToUrl(String ids) {
		String result = "";
		JSONArray jsonArray = new JSONArray();
		String[] items = ids.split(",");
		for (String item : items) {
			String redisKey = "image_" + item;
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("materialId", item);
			// 缓存中读取图片地址
			String imageUrl = redisTemplate.opsForValue().get(redisKey);
			if (!StringUtils.isEmpty(imageUrl)) {
				// 缓存中读取图片
				jsonObject.put("materialUrl", imageUrl);
			}
			else {
				// 数据库获取图片信息
				GoodsMaterial data = this.getById(item);
				if (data != null) {
					redisTemplate.opsForValue().set(item, data.getMaterialUrl());
					jsonObject.put("materialUrl", data.getMaterialUrl());
				}
				else {
					// 数据库不存在图片信息
					jsonObject.put("materialUrl", "");
				}
			}
			jsonArray.add(jsonObject);
		}
		result = JSON.toJSONString(jsonArray);
		return result;
	}

}
